package com.everydayflash.cart.mods {
	import com.as3dmod.ModifierStack;
	import com.as3dmod.modifiers.Break;
	import com.as3dmod.util.Range;
	import com.everydayflash.cart.core.AbstractSketch;
	import com.everydayflash.cart.core.Pixel3D;
	import com.everydayflash.cart.core.util.Obj;
	import com.everydayflash.cart.mods.plugin.LibrarySimple;
	
	import flash.display.BitmapData;
	import flash.events.MouseEvent;
	import flash.geom.Matrix3D;
	import flash.geom.PerspectiveProjection;
	import flash.geom.Vector3D;	

	/**
	 * @author bartekd
	 */
	public class Pants extends AbstractSketch {
		
		private var pts:Vector.<Pixel3D>;
		
		private var projection:Matrix3D;
		private var matrix:Matrix3D;
		private var stack:ModifierStack;
		
		private var leftThigh:Break;
		private var leftCalf:Break;
		private var rightCalf:Break;
		private var rightThigh:Break;
		private var base:Break;
		
		[Embed (source="pants.obj",mimeType="application/octet-stream")]
		private var PantsModel:Class;
		
		private var walking:Walking;

		override protected function init():void {
			walking = new Walking();
			walking.speed = .3;
			
			var p:PerspectiveProjection = new PerspectiveProjection();
			p.focalLength = 400;
			projection = p.toMatrix3D();
			matrix = new Matrix3D();
	
			var obj:Obj = new Obj(new PantsModel());
			pts = obj.getVertices();
			stack = new ModifierStack(new LibrarySimple(), pts);

			leftCalf = new Break(1 - 202/479);
			leftCalf.range = new Range(0,.5);
			stack.addModifier(leftCalf);

			leftThigh = new Break(1 - 395/479);
			leftThigh.range = new Range(0,.5);
			stack.addModifier(leftThigh);
			
			rightCalf = new Break(1 - 202/479);
			rightCalf.range = new Range(.5, 1);
			stack.addModifier(rightCalf);

			rightThigh = new Break(1 - 395/479);
			rightThigh.range = new Range(.5, 1);
			stack.addModifier(rightThigh);
			
			base = new Break(0);
			stack.addModifier(base);
		}

		override protected function mouseDown(e:MouseEvent):void {
		}

		override protected function draw():void {
			walking.speed = mouseX / sw;
			walking.walk();

			base.angle = walking.base;
			leftThigh.angle = walking.leftThigh;
			leftCalf.angle = walking.leftCalf;
			rightThigh.angle = walking.rightThigh;
			rightCalf.angle = walking.rightCalf;
			
			stack.apply();
			
			clearBCanvas();
//			
//			bcanvas.applyFilter(bcanvas, bcanvas.rect, new Point(), new BlurFilter(2,2,1));
//			bcanvas.colorTransform(bcanvas.rect, new ColorTransform(1,1,1,.9));
			
			var angleX:Number = (mouseX - sw/2) / sw/2 * 90;
			var angleY:Number = (mouseY - sh/2) / sh/2 * 90;

			for (var i:int = 0;i < pts.length; i++) {
				matrix.identity();
				matrix.appendRotation(angleX, new Vector3D(0, 1, 0));
				matrix.appendRotation(angleY, new Vector3D(1, 0, 0));
				matrix.appendTranslation(0, 0, 10);
			
				var v:Pixel3D = pts[i];
				
				var iv:Vector3D = matrix.transformVector(v);
				
				var vz:Number = 1 - (iv.z / 1000);
				vz = Math.min(1, vz);
				vz = Math.max(0, vz);
				
				if(iv.z < 0) continue;
				
				iv = projection.transformVector(iv);
				iv.project();

				var c:uint = (vz * 255) << 24 | v.color;
				
				bcanvas.setPixel32(sw / 2 + iv.x, sh/2 + iv.y, c);
			}
		}
	}
}
